/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MainPack;

import MaterialPack.ConveyorBelt;
import MaterialPack.InfoBar;
import MaterialPack.MaterialObject;
import MaterialPack.PetInstance;
import MaterialPack.PetItem;
import Supporter.ImageSupporter;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.Graphics;
import EntityGameObject.*;
import MaterialPack.Button;
import MaterialPack.ContextDialog;
import MaterialPack.InfoPanel;
import javax.obex.PasswordAuthentication;
/**
 *
 * @author Van persie
 */
public class PlayPetCanvas extends AbstractCanvas{
    ConveyorBelt listItem = null;
    PetInstance pet = null;
    
    InfoPanel LVLInfo = null;
    InfoBar EXPbar = null;
    InfoPanel EXPInfo = null;
       
    Button BackBtn = null;    
    
    private Timer timer ;        
    private Sticker sticker ;
    
    public PlayPetCanvas(MainMIDlet midlet)
    { 
        main = midlet;
        height = this.getHeight();
        width = this.getWidth();
        
        try{
            LoadResource( );            
        }catch(Exception e){}
        
        timer = new Timer();
        //StartTimer();
        
    }
    public void StartTimer() {
        sticker = new Sticker();        
        timer.schedule(sticker, 50,50);
    }
    public void PauseTimer()
    {
        sticker.cancel();
    }
    
        
    class Sticker extends TimerTask
        {
            public void run()
            {
                Update();
                repaint();
                
            }
        }
    
    public void LoadResource() throws IOException {     
       if(listObjects != null)
            listObjects.removeAllElements();
        listObjects = new Vector();
        ImageSupporter imgSupporter = new ImageSupporter();
        Image temp = imgSupporter.getImage("/Background.png");
        background = ImageSupporter.scaleImage(temp, width, height);
                
        BackBtn = new Button("/Return.png", "/ReturnPressed.png", "/ReturnPressed.png", null, 3*width/4 , 5, width/4, width/4);
        //listObjects.addElement(BackBtn);
        
        LoadPetInstance();
        LoadConveyorBelt();    
        
    }

    public void LoadPetInstance()
    {
        
        int scalar = 40;
        pet = new PetInstance("/PetPhoto.png", width/6, 20, 2*width/3, 2*width/3,main.getPet());
        LVLInfo = new InfoPanel(null, "Level " + Integer.toString(pet.GetAttributes(Pet.LEVEL)), 5, 5, scalar, 2*scalar/3, Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN  ,Font.SIZE_LARGE));
        LVLInfo.setNoBackground(true);
        LVLInfo.setAnchorMode(InfoPanel.ANC_LEFT);
        EXPInfo = new InfoPanel(null, "EXP ", 5, 2*width/3 + 25, scalar , 2*scalar/3, Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN  ,Font.SIZE_MEDIUM));
        EXPInfo.setNoBackground(true);
        EXPInfo.setAnchorMode(InfoPanel.ANC_LEFT);
        EXPbar = new InfoBar(null, InfoBar.HORIZON, pet.GetAttributes(Pet.EXP), 5 + scalar, 2*width/3 + 25, 3*width/4, 8);
        EXPbar.SetColor(128, 128, 50);
        
        listObjects.addElement(pet);
        listObjects.addElement(LVLInfo);
        listObjects.addElement(EXPInfo);
        listObjects.addElement(EXPbar);
        
    }
    
    public void LoadConveyorBelt()
    {
        listItem = new ConveyorBelt("/SimplePlane.png", null, 0, 2*height/3, width, height/3);
        
        PetItem demoItem = new PetItem("/NormalSound.png", "111", UP, UP, 60, 60,PetItem.TYPE_DRAG);
        listItem.Add(demoItem);
        demoItem = new PetItem("/NormalSound.png", "222", UP, UP, 60, 60,PetItem.TYPE_DRAG);
        listItem.Add(demoItem);
        listItem.ArrangeItem();
        listObjects.addElement(listItem);
        
        listItem.setRun(true);
        
    }
    protected void keyReleased(int key)
    {
        switch(key)
        {            
            case KEY_NUM0:
                main.exitMIDlet();
                break;
            case KEY_NUM1:
                //listItem.setRun(true);
                //StartTimer();
                break;
            case KEY_NUM2:
                PauseTimer();
                break;
        }
        repaint();
    }
    protected void pointerDragged(int x, int y)
    {
         for(int i=0; i <listObjects.size(); i++)
         {
             ((MaterialObject)listObjects.elementAt(i)).Drag(x, y);             
         
         }
         repaint();
    }

    protected void pointerPressed(int x, int y)
    {
         for(int i=0; i <listObjects.size(); i++)
             ((MaterialObject)listObjects.elementAt(i)).Press(x,y);
         BackBtn.Press(x, y);    
         repaint();
    }
    protected void pointerReleased (int x, int y)
    {
        if(listItem.getCurrDrag() != null && pet.isContained(x, y))
        {
            pet.Feed(Pet.EXP, listItem.getCurrDrag().getIncrEXP());
        }
        for(int i=0; i <listObjects.size(); i++)
             ((MaterialObject)listObjects.elementAt(i)).Release(x,y);
        
        if(BackBtn.Release(x, y))  
        {
            PauseTimer();
            main.SwitchTo(MainMIDlet.SCENE_MANAGEMENT);
        }
        
  
        repaint();
    }
    public void Update()
    {
        listItem.Update();
        pet.Update();

        repaint();
    }
    public void paint(Graphics g )
    {
        g.drawImage(background,  0, 0, Graphics.TOP | Graphics.LEFT);
        for(int i=0; i <listObjects.size(); i++)
            ((MaterialObject)listObjects.elementAt(i)).paint(g);
        BackBtn.paint(g);
    }
}
